package com.wteam.dragon.shop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.pojo.bean.FlashPromotion;
import com.wteam.dragon.shop.pojo.bean.FlashPromotionProduct;
import com.wteam.dragon.shop.pojo.bean.FlashPromotionSession;
import com.wteam.dragon.shop.pojo.dto.FlashPromotionProductDto;
import com.wteam.dragon.shop.pojo.vo.FlashPromotionProductVo;
import com.wteam.dragon.shop.pojo.vo.FlashPromotionSessionVo;
import com.wteam.dragon.shop.pojo.vo.FlashPromotionVo;
import com.wteam.dragon.shop.service.FlashPromotionService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/24 14:52
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "秒杀活动管理")
@RequestMapping("flashPromotion")
public class FlashPromotionController {
    private final FlashPromotionService flashPromotionService;

    @Log("添加秒杀活动")
    @ApiOperation("添加秒杀活动")
    @PostMapping("addFlashPromotion")
    @PreAuthorize("@el.check('addFlashPromotion')")
    public ResultMessage addFlashPromotion(FlashPromotionVo flashPromotionVo){
        FlashPromotion flashPromotion = BeanUtil.from(flashPromotionVo, FlashPromotion.class);
        return flashPromotionService.addFlashPromotion(flashPromotion);
    }

    @Log("删除秒杀活动")
    @ApiOperation("删除秒杀活动")
    @DeleteMapping("delFlashPromotion")
    @PreAuthorize("@el.check('delFlashPromotion')")
    public ResultMessage delFlashPromotion(@RequestParam List<Long> ids){
        return flashPromotionService.delFlashPromotion(ids);
    }

    @Log("更改秒杀活动")
    @ApiOperation("更改秒杀活动")
    @PutMapping("editFlashPromotion")
    @PreAuthorize("@el.check('editFlashPromotion')")
    public ResultMessage editFlashPromotion(FlashPromotionVo flashPromotionVo){
        FlashPromotion flashPromotion = BeanUtil.from(flashPromotionVo, FlashPromotion.class);
        return flashPromotionService.editFlashPromotion(flashPromotion);
    }

    @Log("分页获得秒杀活动")
    @ApiOperation("分页获得秒杀活动")
    @GetMapping("pageFlashPromotion")
    @PreAuthorize("@el.check('pageFlashPromotion')")
    public ResultMessage pageFlashPromotion(PageVo<FlashPromotion> pageVo, String title){
        Page<FlashPromotion> page = BeanUtil.fromPage(pageVo);
        return flashPromotionService.pageFlashPromotion(page, title);
    }

    @Log("获得秒杀活动详情")
    @ApiOperation("获得秒杀活动详情")
    @GetMapping("getFlashPromotion")
    @PreAuthorize("@el.check('getFlashPromotion')")
    public ResultMessage getFlashPromotion(Long id){
        return flashPromotionService.getFlashPromotion(id);
    }

    @Log("添加秒杀活动场次")
    @ApiOperation("添加秒杀活动场次")
    @PostMapping("addFlashPromotionSession")
    @PreAuthorize("@el.check('addFlashPromotionSession')")
    public ResultMessage addFlashPromotionSession(FlashPromotionSessionVo flashPromotionSessionVo){
        FlashPromotionSession flashPromotionSession = BeanUtil.from(flashPromotionSessionVo, FlashPromotionSession.class);
        return flashPromotionService.addFlashPromotionSession(flashPromotionSession);
    }

    @Log("删除秒杀活动场次")
    @ApiOperation("删除秒杀活动场次")
    @DeleteMapping("delFlashPromotionSession")
    @PreAuthorize("@el.check('delFlashPromotionSession')")
    public ResultMessage delFlashPromotionSession(Long id){
        return flashPromotionService.delFlashPromotionSession(id);
    }

    @Log("更改秒杀活动场次")
    @ApiOperation("更改秒杀活动场次")
    @PutMapping("editFlashPromotionSession")
    @PreAuthorize("@el.check('editFlashPromotionSession')")
    public ResultMessage editFlashPromotionSession(FlashPromotionSessionVo flashPromotionSessionVo){
        FlashPromotionSession flashPromotionSession = BeanUtil.from(flashPromotionSessionVo, FlashPromotionSession.class);
        return flashPromotionService.editFlashPromotionSession(flashPromotionSession);
    }

    @Log("获得秒杀活动场次列表")
    @ApiOperation("获得秒杀活动场次列表")
    @GetMapping("listFlashPromotionSession")
    @PreAuthorize("@el.check('listFlashPromotionSession')")
    public ResultMessage listFlashPromotionSession(){
        return flashPromotionService.listFlashPromotionSession();
    }

    @Log("通过秒杀活动id获得秒杀活动场次列表")
    @ApiOperation("通过秒杀活动id获得秒杀活动场次列表")
    @GetMapping("listFlashPromotionSessionById")
    @PreAuthorize("@el.check('listFlashPromotionSessionById')")
    public ResultMessage listFlashPromotionSessionById(Long id){
        return flashPromotionService.listFlashPromotionSessionById(id);
    }

    @Log("获得场次详情")
    @ApiOperation("获得场次详情")
    @GetMapping("getFlashPromotionSession")
    @PreAuthorize("@el.check('getFlashPromotionSession')")
    public ResultMessage getFlashPromotionSession(Long id){
        return flashPromotionService.getFlashPromotionSession(id);
    }

    @Log("批量添加秒杀活动商品")
    @ApiOperation("批量添加秒杀活动商品")
    @PostMapping("addFlashPromotionProduct")
    @PreAuthorize("@el.check('addFlashPromotionProduct')")
    public ResultMessage addFlashPromotionProduct(String flashPromotionProductListJson){
        JSONArray jsonArray = JSONUtil.parseArray(flashPromotionProductListJson);
        List<FlashPromotionProduct> list = JSONUtil.toList(jsonArray, FlashPromotionProduct.class);
        return flashPromotionService.addFlashPromotionProduct(list);
    }

    @Log("删除秒杀活动商品")
    @ApiOperation("删除秒杀活动商品")
    @DeleteMapping("delFlashPromotionProduct")
    @PreAuthorize("@el.check('delFlashPromotionProduct')")
    public ResultMessage delFlashPromotionProduct(Long id){
        return flashPromotionService.delFlashPromotionProduct(id);
    }

    @Log("更改秒杀活动商品")
    @ApiOperation("更改秒杀活动商品")
    @PutMapping("editFlashPromotionProduct")
    @PreAuthorize("@el.check('editFlashPromotionProduct')")
    public ResultMessage editFlashPromotionProduct(@Validated FlashPromotionProductVo flashPromotionProductVo) {
        FlashPromotionProduct promotionProduct = BeanUtil.from(flashPromotionProductVo, FlashPromotionProduct.class);
        return flashPromotionService.editFlashPromotionProduct(promotionProduct);
    }

    @Log("分页获得秒杀活动某一活动场次商品")
    @ApiOperation("分页获得秒杀活动某一活动场次商品")
    @GetMapping("pageFlashPromotionProduct")
    @PreAuthorize("@el.check('pageFlashPromotionProduct')")
    public ResultMessage pageFlashPromotionProduct(PageVo<FlashPromotionProductDto> pageVo, Long flashPromotionSessionId, Long flashPromotionId) {
        Page<FlashPromotionProductDto> page = BeanUtil.fromPage(pageVo);
        return flashPromotionService.pageFlashPromotionProduct(page, flashPromotionSessionId, flashPromotionId);
    }

    @Log("获得秒杀活动商品详情")
    @ApiOperation("获得秒杀活动商品详情")
    @GetMapping("getFlashPromotionProduct")
    @PreAuthorize("@el.check('getFlashPromotionProduct')")
    public ResultMessage getFlashPromotionProduct(Long id) {
        return flashPromotionService.getFlashPromotionProduct(id);
    }
}
